package com.example.provametaio;

import java.io.IOException;
import java.security.Provider;

import com.metaio.tools.io.AssetsManager;

import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements LocationListener {

	LocationManager locationManager;
	String provider;
	Location location = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		try {
			AssetsManager.extractAllAssets(this, true); 
		} catch (IOException e) {
			Log.w("ERROREEEEEEEEEEEEEEEE", "WEEEEEEEEEEEEEEEEEEEEEEEEEEE");
		}
		
		locationManager = (LocationManager) getSystemService(getApplicationContext().LOCATION_SERVICE);
		Criteria criteria = new Criteria();
	    provider = locationManager.getBestProvider(criteria, true);
	    ////LOL commit
		Button bottone = (Button) findViewById(R.id.button1);
		
		bottone.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				location = locationManager.getLastKnownLocation(provider);
				if (location != null) {
			        onLocationChanged(location);
				}
				
				//Scommentare per provare metaio
//				Intent intent = new Intent(MainActivity.this, MetaioActivity.class);
				Intent intent = new Intent(MainActivity.this, MetaioActivityAgain.class);
				Bundle b = new Bundle();
				
				if(location != null)
				{
					b.putDouble("lat", (double) (location.getLatitude())); //Your id
					b.putDouble("lng", (double) (location.getLongitude()));
				}	
				intent.putExtras(b); //Put your id to your next Intent
				startActivity(intent);
				finish();
			}
		});
		
		 if (location != null) {
		        onLocationChanged(location);
	      }
	}

	  @Override
	  public void onLocationChanged(Location location) {
		TextView tv1 = (TextView) findViewById(R.id.textView1);
		TextView tv2 = (TextView) findViewById(R.id.textView2);
	    double lat = (double) (location.getLatitude());
	    double lng = (double) (location.getLongitude());
	    tv1.setText(String.valueOf(lat));
	    tv2.setText(String.valueOf(lng));
	  
	  }
	  
	  /* Request updates at startup */
	  @Override
	  protected void onResume() {
	    super.onResume();
	    locationManager.requestLocationUpdates(provider, 400, 1, this);
	  }

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}

}
